

/**
 * 创建时间: 2020/6/28 13:58
 * 作者:lixu
 * 功能描述:处理socket登录逻辑
 * 修改时间:
 * 修改描述:
 */
public class LoginHandler {
    private let TAG = String(describing:LoginHandler.self)

//    public LoginHandler(Looper looper) {
//        super(looper);
//    }

    /***
     * 登录scoket
     *
     * @param initConfigInfo
     * @param identifyReqBuf
     * @param hashCodeBuffer
     * @param reqRespCmdID
     * @return
     */
    //ByteArrayOutputStream
   // public func onLoginSocket( initConfigInfo:InitConfigParams,  identifyReqBuf:Any,  hashCodeBuffer:Any, reqRespCmdID:Int)
    public func onLoginSocket( initConfigInfo:InitConfigParams) -> NSData{
        
        do {
//            var map:[String:Any] = [:];
//            map["token"] = initConfigInfo.token;
//            map["userId"] = initConfigInfo.userId;
//            map["appVersion"] = IMClient.getInstance().getAppVersion();
//
//            map["deviceId"] = IMClient.getInstance().getDeviceId();
//            map["clientType"] = IMClient.getInstance().getClientType();
            let map:[String:Any] =  IMClient.getInstance().getParame();
           
            let jsonData = try JSONSerialization.data(withJSONObject: map, options: .prettyPrinted)
            let json = String(data: jsonData, encoding: String.Encoding.ascii)
//            var json:String = JSON.toJSONString(map);
            IMLog.i(tag:TAG, msg: "getLongLinkIdentifyCheckBuffer params:\(String(describing: json))");

            let data:[UInt8] = ProtocolUtil.packagingToBeByteArraySentMsg(cmdId: ReqCmdID.CMDID_LOGIN_REQ, msg: json!);
            //对应类型
            let dataRaw = NSData.init(bytes: data, length: data.count);
//            identifyReqBuf.write(data);
//            identifyReqBuf.flush();
    //        let bytes = [UInt8](dataRaw)

            return dataRaw
            //ECHECK_NOW;
        } catch let error {
//            e.printStackTrace();
            IMLog.e(tag:TAG, msg:"getLongLinkIdentifyCheckBuffer data error:\(error.localizedDescription)");
            return NSData.init()
            //ECHECK_NEXT;
        }

    }

    /**
     * 登录响应
     *
     * @param initConfigInfo
     * @param buffer
     * @param hashCodeBuffer
     * @param msgCallback
     * @return
     */
    public func onLoginResp( initConfigInfo:InitConfigParams,  buffer:Data,  hashCodeBuffer:Data,   msgCallback:AnyObject?) -> Bool{
        
        let respId:Int = ProtocolUtil.getReceiveCmdId(buffer: [UInt8](buffer));
        let isSuccess:Bool = (RespSuccCmdID.init().CMDID_LOGIN_SUCC_RESP == respId)
        IMLog.i(tag:TAG, msg:"onLongLinkIdentifyResp login isSuccess:\(isSuccess)  respId:\(respId)  isInRoom:\(IMClient.getInstance().getIsInRoom())");
        let hadLoginSuccess:Bool = IMClient.getInstance().getIsLoginSuccess();
        
        ///登录成功
        if (isSuccess) {
            IMClient.getInstance().setLoginSuccess(loginSuccess: true);
            
            ///之前已经登录成功过,再次登录成功,当成断线重连成功.
            
            if (hadLoginSuccess && msgCallback != nil) {
                //房间内断线重连后必须重新调用进入房间命令,否则后端会将当前用户移除房间
                IMLog.i(tag: TAG, msg: "断线重连登录成功,是否在房间内:\(IMClient.getInstance().getIsInRoom())");
                //房间内断线重连登陆成功
                let callback = msgCallback as! IMsgCallBack
                callback.onReconnected();
            }else{
                //登录成功
                let callback = msgCallback as! IMsgCallBack
                callback.onLoginSuccess();
                
            }
        }else{
            let callback = msgCallback as! IMsgCallBack
            callback.onLoginFail(respId: respId, initConfigParams: initConfigInfo);
        }
        
        
//        post(new Runnable() {
//            @Override
//            public void run() {
//                //是否已经登录成功过
//                boolean hadLoginSuccess = IMClient.getInstance().isLoginSuccess();
//
//                ///登录成功
//                if (isSuccess) {
//                    IMClient.getInstance().setLoginSuccess(true);
//
//                    ///之前已经登录成功过,再次登录成功,当成断线重连成功.
//                    if (hadLoginSuccess && msgCallback != null) {
//                        //房间内断线重连后必须重新调用进入房间命令,否则后端会将当前用户移除房间
//                        IMLog.i(TAG, "断线重连登录成功,是否在房间内:" + IMClient.getInstance().isInRoom());
//                        //房间内断线重连登陆成功
//                        msgCallback.onReconnected();
//                    }
//                }
//            }
//        });
        return isSuccess;
    }

}
